package com.oplink.webems.modules.log.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.oplink.webems.common.utils.DateUtils;
import com.oplink.webems.common.utils.UserUtils;
import com.oplink.webems.modules.log.entity.CommLog;
import com.oplink.webems.modules.log.service.CommLogService;
import com.oplink.webems.modules.sys.entity.User;

@Aspect
@Component
public class CommLogAop extends BaseLogAop {
	
	static Logger l = Logger.getLogger(CommLogAop.class);
	
	@Autowired
	CommLogService cs;

	@Pointcut("execution(* com.oplink.webems.common.snmp.service.ISnmpService.*(..))")
	public void serviceAop(){}
	
	@Pointcut("execution(* com.oplink.webems.common.snmp.service.TrapService.processPdu(..))")
	public void trapAop(){}
	
	@Before("serviceAop()")
	public void writeCommLog(JoinPoint joinPoint){
		Signature s = joinPoint.getSignature();
		Object[] args = joinPoint.getArgs();
		
		CommLog cl = new CommLog();
		try{
			cl.setUser(new User(UserUtils.getCurrentUserId()));
		}catch(Exception e){
			l.info(e.getMessage());
			cl.setUser(new User(1L));
		}
		//SnmpService target = (SnmpService)joinPoint.getTarget();
		cl.setIp("172.16.166.240");
		cl.setParams(getParams(args));
		cl.setDesci(getDesc(s));
		cl.setTimei(DateUtils.getCalendar(DateUtils.getDateTime()));
		
		if(getDesc(s).startsWith("get")){
			cl.setType(11);
		}else if(getDesc(s).startsWith("set")){
			cl.setType(12);
		}else{
			return;
		}
		cs.saveCommLog(cl);
	}
	
	@Before("trapAop()")
	public void writeTrapCommLog(JoinPoint joinPoint){
		Signature s = joinPoint.getSignature();
		Object[] args = joinPoint.getArgs();
		
		CommLog cl = new CommLog();
		cl.setUser(new User(1L));
		//SnmpService target = (SnmpService)joinPoint.getTarget();
		cl.setIp("172.16.166.240");
		cl.setParams(getParams(args));
		cl.setDesci(getDesc(s));
		cl.setTimei(DateUtils.getCalendar(DateUtils.getDateTime()));
		cl.setType(1);
		cs.saveCommLog(cl);
	}
	
	@AfterThrowing(pointcut="serviceAop()", throwing="ex")
	public void aroundException(JoinPoint joinPoint, Exception ex){
		Signature s = joinPoint.getSignature();
		Object[] args = joinPoint.getArgs();
		joinPoint.getTarget();
		joinPoint.getThis();
		
		CommLog cl = new CommLog();
		
		cl.setIp("172.16.166.240");
		cl.setParams(getParams(args));
		cl.setDesci(getDesc(s));
		cl.setTimei(DateUtils.getCalendar(DateUtils.getDateTime()));
		
		if(getDesc(s).startsWith("get")){
			cl.setType(11);
		}else if(getDesc(s).startsWith("set")){
			cl.setType(12);
		}else{
			return;
		}
		
		try{
			cl.setUser(new User(UserUtils.getCurrentUserId()));
		}catch(Exception e){
			l.info(e.getMessage());
			cl.setUser(new User(1L));
		}
		cl.setSrc(ex.getMessage());
		cs.saveCommLog(cl);
		
	}
}
